<?php
/**
* * Model Class
* 
* @author Guillermo Landín
* @link 
* 
* 
*/ 

class Portfoliomodel extends Model {

    function __construct()
    {
        parent::Model();
        
        $this->CI =& get_instance(); 
    }

    //returns all the details for single item $id for display in portfolio detail page
    public function get_item($id = NULL, $fields='*'){

        $this->db->select($fields)
                ->from('portfolio')
                ->where('id_Portfolio', $id)
                ->where('Active_Portfolio', 1);       

        $query = $this->db->get();
        return $query->row_array();

    }

    //returns the list of products used in portfolio
    public function get_products($id=NULL, $fields="products.id_Products, products.Name_Products"){

        $this->db->select($fields)
                ->from('portfolios_products')
                ->join('products', 'portfolios_products.id_Products = products.id_Products', 'left')
                ->where('id_Portfolio', $id)
                ->where('products.Active_Products', 1);  
        
        $query = $this->db->get();
        $result = $query->result_array();
        
        return $result;
      
    }
    
    //returns the list of portfolio. if $id is received, that item will be excluded
    function get_list($id = FALSE){
    
        $this->db->select('id_Portfolio AS id, Name_Portfolio AS title, Introduction_Portfolio AS text, Main_Image AS image')
                ->from('portfolio')
                ->where('Active_Portfolio', 1)
                ->order_by('Name_Portfolio', 'ASC');  

        if ($id) $this->db->where('id_Portfolio !=', $id);

        $query = $this->db->get();

        $result = $query->result_array();
                                 
        return $result;

    }

    // returns a simple array of filenames
    public function get_images($id = FALSE){

        $this->db->select("*")
                ->from('portfolios_images')       
                ->where('id_Portfolio', $id);  
        
        $query = $this->db->get();
        $result = $query->result_array();

        foreach($result as &$row){
            $row = $row['Image'];
        }   

		return $result;

    }         


    // receives a simple array of product ids and returns an associative array of ids and names
    public function get_products_data($ids = array()){

        if (empty($ids)) return $ids;
        $this->db->select("id_Products, Name_Products")
                ->from('products')  
                ->where_in('id_Products', $ids)
                ->where('Active_Products', 1); 
        
        $query = $this->db->get();
        return $query->result_array();

    }         



/***********************************   
*   Sub-menu functions
***********************************/

    //returns the full submenu
    public function get_submenu(){
    
        $this->db->select('id_Portfolio, Name_Portfolio AS name, Description_Portfolio', FALSE)
                ->from('portfolio')
                ->where('Active_Portfolio', 1)
                ->order_by('Name_Portfolio', 'ASC');  

        $query = $this->db->get();

        $result = $query->result_array();

        foreach($result as &$row){
            $row = array( 
                        'name' => $row['name'], 
                        'controller' => 'portfolio/view/'.$row['id_Portfolio'], 
                        'param' => $row['id_Portfolio']
                        );
        }
                                 
        return $result;

    }
    
    //returns the submenu with one item marked as selected
    public function get_submenu_by_id($id = FALSE){

        //get the full array of months
        $result = $this->get_submenu();  

        //set the selected item
        foreach($result as &$item){

            if ($item['param'] == $id) $item['selected'] = TRUE;

        }

        return $result;

    }

/***********************************
*   Backoffice functions
***********************************/

    //returns the list of portfolio
    function admin_get_list($fields='*'){
    
        $this->db->select($fields)
                ->from('portfolio')
                ->order_by('Name_Portfolio', 'ASC');  

        $query = $this->db->get();

        $result = $query->result_array();
                                 
        return $result;

    }  
    public function admin_get_item($id=FALSE, $fields='*'){

        $this->db->select($fields)
                ->from('portfolio')       
                ->where('id_Portfolio', $id);  
        
        $query = $this->db->get();
        return $query->row_array();

    }  
    public function admin_get_products($id=FALSE, $fields="*"){

        $this->db->select($fields)
                ->from('portfolios_products')       
                ->where('id_Portfolio', $id);  
        
        $query = $this->db->get();
        $result = $query->result_array();
        
        foreach($result as &$row){
            $row = $row['id_Products'];
        }

        return $result;

    }
    public function admin_update_portfolio($id=FALSE, $data=array()){

        $this->db->where('id_Portfolio', $id)
                ->update('portfolio', $data); 

    }                      
    public function admin_insert_portfolio($data=array()){

        $data["id_Portfolio"] = uniqid();
        $data["Active_Portfolio"] = 0;
        $this->db->insert('portfolio', $data); 
        return $data["id_Portfolio"];

    }  
    public function admin_delete_portfolio($id=FALSE){

        $this->db->where('id_Portfolio', $id);
        $this->db->delete('portfolio'); 

    }    
    public function admin_delete_images($id=FALSE){

        $this->db->where('id_Portfolio', $id);
        $this->db->delete('portfolios_images'); 

    }
    public function admin_delete_products($id=FALSE){

        $this->db->where('id_Portfolio', $id);
        $this->db->delete('portfolios_products'); 

    }    
    public function admin_insert_images($id=FALSE, $data=array()){

        foreach($data as $item){                      
            $row['Image'] = $item;
            $row['id_Portfolio'] = $id;
            $this->db->insert('portfolios_images', $row);
        }
    }   
    public function admin_insert_products($id=FALSE, $data=array()){

        foreach($data as $item){
            $row['id_Products'] = $item;
            $row['id_Portfolio'] = $id;
            $this->db->insert('portfolios_products', $row);
        }
    }   

}
?>